package com.sagereal.wtc.factorymode.Activity;

import androidx.appcompat.app.ActionBar;

import android.content.Intent;
import android.content.res.Configuration;
import android.content.res.Resources;
import android.os.Bundle;
import android.util.Log;
import android.view.MenuItem;
import android.widget.ListView;

import com.sagereal.wtc.factorymode.Adapter.ItemAdapter;
import com.sagereal.wtc.factorymode.db.DatabaseManager;
import com.sagereal.wtc.factorymode.R;

import java.util.ArrayList;
import java.util.List;
import java.util.Locale;

public class ItemTestActivity extends BaseActivity {
    private static final int REQUEST_CODE = 1; // 请求码
    private ListView mListView;
    private List<Item> mItemList;
    private ItemAdapter mAdapter;
    private DatabaseManager mDbManager;

    @Override
    protected void onCreate(Bundle savedInstanceState) {
        super.onCreate(savedInstanceState);
        ActionBar actionBar = getSupportActionBar();
        if (actionBar != null) {
            actionBar.setHomeButtonEnabled(true);
            actionBar.setDisplayHomeAsUpEnabled(true);  // 添加返回的图标
        }
        setContentView(R.layout.activity_item_test);
        setTitle(getString(R.string.item_test));

        // 初始化数据库管理器
        mDbManager = new DatabaseManager(this);
        initView();

        Intent intent = getIntent();
        if (intent != null) {
            int itemId = intent.getIntExtra("ItemId", -1);
            String status = intent.getStringExtra("status");
            String itemCheck = intent.getStringExtra("check");
            // 确保itemId在有效范围内，并且status和itemCheck不为null
            if (itemId >= 0 && itemId < mItemList.size() && status != null && itemCheck != null) {
                mItemList.get(itemId).setStatus(status);
                mItemList.get(itemId).setCheck(itemCheck);
                mDbManager.saveResult(mItemList.get(itemId).getName(), status, itemCheck);
                mAdapter.notifyDataSetChanged();
            }
        }
    }

    private void initView() {
        mListView = findViewById(R.id.item_view);
        mItemList = new ArrayList<>();

        mItemList.add(new Item(getString(R.string.title_battery_test), "untest", "battery"));
        mItemList.add(new Item(getString(R.string.title_vibration_test), "untest", "vibration"));
        mItemList.add(new Item(getString(R.string.title_mic_test), "untest", "mic"));
        mItemList.add(new Item(getString(R.string.title_headset_test), "untest", "headset"));
        mItemList.add(new Item(getString(R.string.title_lcd_test), "untest", "lcd"));
        mItemList.add(new Item(getString(R.string.title_speaker_test), "untest", "speaker"));
        mItemList.add(new Item(getString(R.string.title_receiver_test), "untest", "receiver"));
        mItemList.add(new Item(getString(R.string.title_camera_test), "untest", "camera"));
        mItemList.add(new Item(getString(R.string.title_flash_test), "untest", "flash"));
        mItemList.add(new Item(getString(R.string.title_key_test), "untest", "key"));
        mItemList.add(new Item(getString(R.string.title_brightness_test), "untest", "brightness"));

        mAdapter = new ItemAdapter(ItemTestActivity.this, R.layout.item, mItemList);
        mListView.setAdapter(mAdapter);

        loadResultsFromDatabase();

        mListView.setOnItemClickListener((adapterView, view, i, l) -> {
            Intent intent = new Intent();
            switch (i) {
                case 0:
                    intent.setClass(ItemTestActivity.this, BatteryActivity.class); // 电池测试
                    break;
                case 1:
                    intent.setClass(ItemTestActivity.this, VibrationActivity.class); // 振动测试
                    break;
                case 2:
                    intent.setClass(ItemTestActivity.this, MicActivity.class); // 麦克测试
                    break;
                case 3:
                    intent.setClass(ItemTestActivity.this, HeadsetActivity.class); // 耳机测试
                    break;
                case 4:
                    intent.setClass(ItemTestActivity.this, LcdActivity.class); // Lcd测试
                    break;
                case 5:
                    intent.setClass(ItemTestActivity.this, SpeakerActivity.class); // 喇叭测试
                    break;
                case 6:
                    intent.setClass(ItemTestActivity.this, ReceiverActivity.class); // 听筒测试
                    break;
                case 7:
                    intent.setClass(ItemTestActivity.this, CameraActivity.class); // 相机测试
                    break;
                case 8:
                    intent.setClass(ItemTestActivity.this, FlashActivity.class); // 闪光灯测试
                    break;
                case 9:
                    intent.setClass(ItemTestActivity.this, KeyActivity.class); // 按键测试
                    break;
                case 10:
                    intent.setClass(ItemTestActivity.this, BrightnessActivity.class); // 亮度测试
            }

            // 在每个case分支中传递数据
            intent.putExtra("ItemId", i); // 传递当前点击的项的位置或ID
            intent.putExtra("status", mItemList.get(i).getStatus()); // 传递当前项的状态
            intent.putExtra("check", mItemList.get(i).getCheck()); // 传递当前项的check
            startActivityForResult(intent, REQUEST_CODE);
        });
    }

    @Override
    protected void onActivityResult(int requestCode, int resultCode, Intent data) {
        super.onActivityResult(requestCode, resultCode, data);
        if (requestCode == REQUEST_CODE && resultCode == RESULT_OK) {
            if (data != null) {
                int itemId = data.getIntExtra("ItemId", -1);
                String status = data.getStringExtra("status");
                String check = data.getStringExtra("check");
                // 更新数据
                updateItemStatus(itemId, status, check);
                // 重新加载数据并刷新UI
                loadResultsFromDatabase();
            }
        }
    }
    private void updateItemStatus(int itemId, String status, String check) {
        if (itemId >= 0 && itemId < mItemList.size()) {
            Item item = mItemList.get(itemId);
            item.setStatus(status);
            item.setCheck(check);
            mDbManager.saveResult(item.getName(), status, check);
        }
    }
    private void loadResultsFromDatabase() {
        for (Item item : mItemList) {
            String status = mDbManager.getResult(item.getCheck());
            item.setStatus(status);
        }
        mAdapter.notifyDataSetChanged();
    }

    @Override
    public boolean onOptionsItemSelected(MenuItem item) {
        if (item.getItemId() == android.R.id.home) {
            this.finish();
            return true;
        }
        return super.onOptionsItemSelected(item);
    }

    @Override
    protected void onDestroy() {
        super.onDestroy();
        mDbManager.close();
    }
}
